package com.ruoyi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.domain.School;
import com.ruoyi.service.ISchoolService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * @version 1.0
 * @ClassName
 * @Author DELL
 * @Date 2020/6/28 16:44
 **/

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class SchoolTestApplication {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ISchoolService schoolService;


    private static JSONObject areaMap;

    static {
        JSONObject jsonObject = new JSONObject();
        jsonObject.fluentPut("玄武区","320102");
        jsonObject.fluentPut("秦淮区","320104");
        jsonObject.fluentPut("建邺区","320105");
        jsonObject.fluentPut("鼓楼区","320106");
        jsonObject.fluentPut("浦口区","320111");
        jsonObject.fluentPut("栖霞区","320113");
        jsonObject.fluentPut("雨花台区","320114");
        jsonObject.fluentPut("江宁区","320115");
        jsonObject.fluentPut("六合区","320116");
        jsonObject.fluentPut("溧水区","320117");
        jsonObject.fluentPut("高淳区","320118");

        areaMap = jsonObject;
    }

    @Test
    public void insertSchoolList() throws InterruptedException {
        getSchools(1);

    }



    public void getSchools(int count) throws InterruptedException {

        String url = "http://api.map.baidu.com/place/v2/search?query=大学&region=南京&output=json&ak=nMgIG8pC7YKcTysHAyoyx71oy9mMyH6h&page_num="+(count)+"&page_size=10";


        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);


        JSONObject result = JSONObject.parseObject(forEntity.getBody());



        String resultData = result.getString("results");

        JSONArray jsonArray = JSONObject.parseArray(resultData);


        List<School> schoolList = new ArrayList<>();


        Iterator<Object> iterator = jsonArray.iterator();
        while (iterator.hasNext()){
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(iterator.next()));

            School school = new School();
            school.setSchoolName(jsonObject.getString("name"));
            school.setSchoolAddress(jsonObject.getString("address"));
            school.setAdvisoryPhone(jsonObject.getString("telephone"));
            school.setSchoolArea(areaMap.getString(jsonObject.getString("area")));

            String lat = "";
            String lng = "";

            JSONObject location = JSONObject.parseObject(jsonObject.getString("location"));
            lat = location.getString("lat");
            lng = location.getString("lng");

            school.setSchoolLat(lat);
            school.setSchoolLng(lng);

            school.setSchoolType("5");

            schoolList.add(school);

        }


        schoolList.forEach(s->schoolService.insertSchool(s));


        if (jsonArray.size() > 0){
            Thread.sleep(5000L);
            getSchools(++count);
        }

    }


}
